package com.xunshangwang.pinhaohuo.tools;

import android.content.Context;

import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xunshangwang.pinhaohuo.entity.WechatPay;

import java.util.Map;

/**
 * Created by xzp on 16/2/25.
 */
public class PayHelper {
    private static OnPayCallback mOnPayCallback;
    public static final int PAY_FAIL=-1;
    public static final int PAY_SUCCESS=1;
    public static final int PAY_CANCEL=0;
    public static final int PAY_OTHER=2;
    public static final int WAY_WECHAT=0;
    public static final String WX_APP_ID="wxed5a65b2777ab93d";
    private IWXAPI mIwxapi;
    //public static final Map<String,OnPayCallback>
    public PayHelper(Context context){
        mIwxapi = WXAPIFactory.createWXAPI(context, null);
        mIwxapi.registerApp(WX_APP_ID);
    }
    public final boolean isWechatSupport(){
        return mIwxapi.isWXAppInstalled() && mIwxapi.isWXAppSupportAPI();
    }
    public final void startWechatPay(WechatPay wechatPay,OnPayCallback callback){
        if(isWechatSupport()) {
            this.registerCallback(callback);
            PayReq req = new PayReq();
            req.appId = wechatPay.getAppId();
            req.packageValue = wechatPay.getPackAge();
            req.partnerId = wechatPay.getPartnerId();
            req.timeStamp = wechatPay.getTimeStamp();
            req.nonceStr = wechatPay.getNonceStr();
            req.prepayId = wechatPay.getPrepay_id();
            req.sign = wechatPay.getPaySign();
            mIwxapi.sendReq(req);
        }
    }
    public final static OnPayCallback getCallBack(){
        return mOnPayCallback;
    }
    public final static void registerCallback(OnPayCallback callback){
        mOnPayCallback=callback;
    }
    public final static void unRegisterCallback(){
        mOnPayCallback=null;
    }
    public interface OnPayCallback{
        void onPayComplete(int payWay,int code,String msg);
    }
}
